package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogArticle;
import com.ruoyi.blog.service.IBlogArticleService;
import com.ruoyi.blog.service.IBlogCommentService;
import com.ruoyi.common.annotation.NoRepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 博客文章Controller
 *
 * @author 刘哲贤
 * @date 2022-08-19
 */
@RestController
@RequestMapping("/front")
public class FrontController extends BaseController {

    @Resource
    private IBlogArticleService blogArticleService;
    @Resource
    private IBlogCommentService blogCommentService;

    /**
     * 博客端获取文章列表分页
     *
     * @param blogArticle
     * @return
     */
    @GetMapping("/articles/list")
    public TableDataInfo list(BlogArticle blogArticle) {
        blogArticle.setDeleted(BlogArticle.NOT_DELETED);
        blogArticle.setIsRelease(BlogArticle.IS_RELEASE);
        startPage();
        List<BlogArticle> list = blogArticleService.selectBlogArticleListFront(blogArticle);
        return getDataTable(list);
    }

    /**
     * 前端根据id获取博客
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/articles/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(blogArticleService.getBlogArticleById(id));
    }

    /**
     * 防止重复提交的演示代码
     *
     * @return
     */
    @GetMapping("/test")
    @NoRepeatSubmit(name = "test", seconds = 10)
    public AjaxResult noRepeatSubmit() {
        return AjaxResult.success("请求成功");
    }


    /**
     * 根据文章id获取评论列表
     *
     * @return
     */
    @GetMapping("/getComments/{articleId}")
    public AjaxResult getCommentList(@PathVariable("articleId") Integer articleId) {
        return AjaxResult.success(blogCommentService.commentList(articleId));
    }
}
